﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bai1
{
    public partial class Form1 : Form
    {
        List<Customer> Custoemer = new List<Customer>();
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            //khai báo nguồn dữ liệu
            int[] mang = { 3, 5, 6, 8, 3, 7, 9, 0 };
            //tạo truy vấn
            IEnumerable<int> query = from n in mang
                                     where n < 4
                                     orderby n descending
                                     select n;
            //thực thi truy vấn 
            StringBuilder kq = new StringBuilder();
            foreach (var pt in query)
            {
                kq.AppendLine(pt.ToString());
                textBox1.Text = kq.ToString();

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            #region "Khai Báo Dữ Liệu"
            List<Parts> parts = new List<Parts>();

            Parts p1 = new Parts();
            p1.PartNumber = 1;
            p1.PartDescription = "Cog";
            parts.Add(p1);

            Parts p2 = new Parts();
            p2.PartNumber = 2;
            p2.PartDescription = "Widget";
            parts.Add(p2);

            Parts p3 = new Parts();
            p3.PartNumber = 3;
            p3.PartDescription = "Gear";
            parts.Add(p3);

            Parts p4 = new Parts();
            p4.PartNumber = 4;
            p4.PartDescription = "Tank";
            parts.Add(p4);

            Parts p5 = new Parts();
            p5.PartNumber = 5;
            p5.PartDescription = "Piston";
            parts.Add(p5);

            Parts p6 = new Parts();
            p6.PartNumber = 6;
            p6.PartDescription = "Shaft";
            parts.Add(p6);

            Parts p7 = new Parts();
            p7.PartNumber = 7;
            p7.PartDescription = "Pulley";
            parts.Add(p7);

            Parts p8 = new Parts();
            p8.PartNumber = 8;
            p8.PartDescription = "Sprocket";
            parts.Add(p8);
            #endregion

            IEnumerable<Parts> query = from pa in parts
                                       where pa.PartNumber > 3 
                                       select pa;
            //thực thi truy vấn 

            StringBuilder str = new StringBuilder();
            foreach (var item in query)
            {
                str.AppendLine(item.PartNumber.ToString() + ":"+ item.PartDescription);
                textBox1.Text = str.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            IEnumerable<Customer> query = from c in Custoemer
                                          select c;
            //thực thi truy vấn 

            StringBuilder str = new StringBuilder();
            foreach (var item in query)
            {
                str.AppendLine(item.CustomersID.ToString() + ":" + item.ContactName +":" +item.CompanyName +"/n");
                
            }
            textBox1.Text += str.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            LayDanhSach();
            IEnumerable<Customer> query = from c in Custoemer
                                          where c.CompanyName.Contains("Lon Don")
                                          select c;
            //thực thi truy vấn 

            StringBuilder str = new StringBuilder();
            foreach (var item in query)
            {
                str.AppendLine(item.CustomersID.ToString() + ":" + item.ContactName + ":" + item.CompanyName + "/n");

            }
            textBox1.Text += str.ToString();
        }
        public void LayDanhSach()
        {
           
            Customer c1 = new Customer();
            c1.CustomersID = 1;
            c1.ContactName = "BAO";
            c1.CompanyName = "Lon Don";
            Custoemer.Add(c1);

            Customer c2 = new Customer();
            c2.CustomersID = 2;
            c2.ContactName = "BAOdssd";
            c2.CompanyName = "Tuy Phong";
            Custoemer.Add(c2);

            Customer c3 = new Customer();
            c3.CustomersID = 3;
            c3.ContactName = "BAOsdf";
            c3.CompanyName = "Binh THuan ";
            Custoemer.Add(c3);

            Customer c4 = new Customer();
            c4.CustomersID = 4;
            c4.ContactName = "AAAAAAA";
            c4.CompanyName = "bbbbbbbbb";
            Custoemer.Add(c4);

            Customer c5 = new Customer();
            c5.CustomersID = 5;
            c5.ContactName = "CCCCCCCCC";
            c5.CompanyName = "iiooiuio";
            Custoemer.Add(c5);

        }
    }
}
